﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text;

using KF.DHLModel;
using KF.DHLBL;

namespace KF.Servers.OrderInfo
{
    /// <summary>
    /// Summary description for OrderService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class OrderService : System.Web.Services.WebService
    {

        /// <summary>
        /// 批量导入订单信息
        /// </summary>
        /// <param name="ordersList"></param>
        /// <returns></returns>
        [Obsolete("批量导入功能已经废弃，建议不再使用")]
        [WebMethod]
        public string ImportOrders(List<Orders> ordersList)
        {
            OrdersBLL ordersBll = new OrdersBLL();
            return ordersBll.ImportOrders(ordersList);
        }

        /// <summary>
        /// 导入订单信息
        /// </summary>
        /// <param name="orders"></param>
        /// <returns></returns>
        [WebMethod]
        public string ImportOrder(Orders orders)
        {
            OrdersBLL ordersBll = new OrdersBLL();
            return ordersBll.AddOrder(orders);
        }

        /// <summary>
        /// 查询订单信息
        /// </summary>
        /// <param name="channelCode"></param>
        /// <param name="customerCode"></param>
        /// <param name="orderTimeBegin"></param>
        /// <param name="orderTimeEnd"></param>
        /// <param name="orderNumbers"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [WebMethod]
        public List<Orders> SearchOrders(string channelCode, string customerCode, DateTime orderTimeBegin, DateTime orderTimeEnd, string[] orderNumbers, int? status, string batchInfo)
        {
            OrdersBLL ordersBll = new OrdersBLL();
            StringBuilder sbSqlQuery = new StringBuilder();

            sbSqlQuery.Append(" 1 = 1 ");

            if (!string.IsNullOrEmpty(channelCode))
                sbSqlQuery.Append(string.Format(" AND ChannelCode like '*{0}*' ", channelCode));
            if (!string.IsNullOrEmpty(customerCode))
                sbSqlQuery.Append(string.Format(" AND CustomerCode like '*{0}*' ", customerCode));
            if (orderTimeBegin != DateTime.MinValue)
                sbSqlQuery.Append(string.Format(" AND OrderTime >= #{0}# ", orderTimeBegin.ToString("yyyy-MM-dd 00:00:00")));
            if (orderTimeEnd != DateTime.MinValue)
                sbSqlQuery.Append(string.Format(" AND OrderTime <= #{0}# ", orderTimeEnd.ToString("yyyy-MM-dd 23:59:59")));
            if (orderNumbers.Length > 0)
            {
                StringBuilder orderNumberSB = new StringBuilder();
                foreach (string temp in orderNumbers)
                {
                    orderNumberSB.Append("'" + temp + "',");
                }
                sbSqlQuery.Append(string.Format(" AND OrderNumber in ({0}) ", orderNumberSB.ToString().TrimEnd(',')));
            }
            if (status.HasValue)
                sbSqlQuery.Append(string.Format(" AND Status = {0} ", status.Value));
            if (!string.IsNullOrEmpty(batchInfo))
                sbSqlQuery.Append(string.Format(" AND BatchInfo = '{0}'", batchInfo));


            return ordersBll.GetModelList(sbSqlQuery.ToString());
        }

        /// <summary>
        /// 新增订单信息（默认采用导入相同方法）
        /// </summary>
        /// <param name="orders"></param>
        /// <returns></returns>
        [WebMethod]
        public string AddOrder(Orders orders)
        {
            OrdersBLL ordersBll = new OrdersBLL();
            return ordersBll.AddOrder(orders);
        }

        /// <summary>
        /// 修改订单信息
        /// </summary>
        /// <param name="orders"></param>
        /// <returns></returns>
        [WebMethod]
        public string EditOrder(Orders orders)
        {
            OrdersBLL ordersBll = new OrdersBLL();
            return ordersBll.EditOrder(orders);
        }

        /// <summary>
        /// 录入批次信息
        /// </summary>
        /// <param name="orderNumbers"></param>
        /// <param name="batchInfo"></param>
        /// <param name="flightInfo"></param>
        /// <param name="flihtTime"></param>
        /// <returns></returns>
        [WebMethod]
        public string EditOrderBatchInfo(string[] orderNumbers, string batchInfo, string flightInfo, DateTime flihtTime)
        {
            OrdersBLL ordersBll = new OrdersBLL();
            return ordersBll.EditOrderBatchInfo(orderNumbers, batchInfo, flightInfo, flihtTime);
        }

        /// <summary>
        /// 发运订单
        /// </summary>
        /// <param name="orderNumbers"></param>
        /// <returns></returns>
        [WebMethod]
        public string ShippingOrders(string[] orderNumbers)
        {
            OrdersBLL ordersBll = new OrdersBLL();
            return ordersBll.ShippingOrders(orderNumbers);
        }
    }
}
